<?php

namespace app\adminapi\logic\game;

use app\common\logic\BaseLogic;
use app\common\model\game\Banner;
use app\common\model\game\Software;
use app\common\service\FileService;

/**
 * 管理逻辑
 * Class BannerLogic
 * @package app\adminapi\logic\game
 */
class BannerLogic extends BaseLogic
{

    /**
     * @notes  添加
     * @param array $params
     * @date 2024/7/20 9:57
     */
    public static function add(array $params)
    {
        try {
            $title = $params['title'];
            $image = $params['image'] ? FileService::setFileUrl($params['image']) : '';
            $out_url = $params['out_url'] ?? '';
            $resource_id = $params['resource_id'] ?? 0;
            $operator = $params['operator'];
            $where = [
                ['title', '=', $title],
                ['delete_time', '=', NULL]
            ];
            $exsit_banner = Banner::where($where)->findOrEmpty();
            if($exsit_banner->isExists()){
                throw new \Exception('该标题已存在，不能重复添加');
            }

            Banner::create([
                'title' => $title,
                'image' => $image,
                'out_url' => $out_url,
                'resource_id' => $resource_id,

                'is_show' => $params['is_show'] ?? 0,
                'sort' => $params['sort'] ?? 0, // 排序
                'operator' => $operator
            ]);

            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes  编辑
     * @param array $params
     * @return bool
     * @date 2024/7/20 10:12
     */
    public static function edit(array $params) : bool
    {
        try {
            //当前id
            $id = $params['id'];
            $title = $params['title'];
            $image = $params['image'] ? FileService::setFileUrl($params['image']) : '';
            $out_url = $params['out_url'] ?? '';
            $resource_id = $params['resource_id'] ?? 0;
            $operator = $params['operator'];

            $where = [
                ['title', '=', $title],
                ['delete_time', '=', NULL],
                ['id', '<>', $id]
            ];
            $exsit_banner = Banner::where($where)->findOrEmpty();
            if($exsit_banner->isExists()){
                throw new \Exception('该标题已存在，不能重复添加');
            }

            Banner::update([
                'id' => $id,
                'title' => $title,
                'image' => $image,
                'out_url' => $out_url,
                'resource_id' => $resource_id,

                'is_show' => $params['is_show'] ?? 0,
                'sort' => $params['sort'] ?? 0, // 排序
                'operator' => $operator,
                'update_time' => time()
            ]);

            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes  删除
     * @param array $params
     * @date 2024/7/20 10:17
     */
    public static function delete(array $params)
    {
        try {
            //软删除
            $id = $params['id'];
            Banner::destroy($id);
            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

    /**
     * @notes  查看详情
     * @param $params
     * @return array
     * @date 2024/7/20 10:15
     */
    public static function detail($params) : array
    {
        $info = Banner::findOrEmpty($params['id'])
            ->append(['show_desc'])
            ->toArray();
        return $info;
    }

    /**
     * @notes  更改上下架
     * @param array $params
     * @return bool
     * @date 2024/7/19 10:18
     */
    public static function updateStatus(array $params)
    {
        Banner::update([
            'id' => $params['id'],
            'is_show' => $params['is_show']
        ]);
        return true;
    }

    /**
     * @notes 数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAllData()
    {
        return Banner::order(['sort' => 'desc', 'id' => 'desc'])
            ->select()
            ->toArray();
    }
}